package com.tnxst.phexamination.activity;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.SavedStateViewModelFactory;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.tnxst.phexamination.R;
import com.tnxst.phexamination.adapter.ViewPagerAdapter;
import com.tnxst.phexamination.databinding.ActivityIndexBinding;
import com.tnxst.phexamination.fragment.IndexFragment;
import com.tnxst.phexamination.fragment.MyFragment;
import com.tnxst.phexamination.viewmodel.GlobalViewModel;

import java.util.ArrayList;
import java.util.List;

public class IndexActivity extends BaseActivity implements ViewPager.OnPageChangeListener {

    private GlobalViewModel viewModel;

    private ActivityIndexBinding binding;

    private List<Fragment> fragments;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        viewModel = new ViewModelProvider(this, new SavedStateViewModelFactory(getApplication(), this)).get(GlobalViewModel.class);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_index);
        binding.setIndex(viewModel);
        binding.setLifecycleOwner(this);
        initView();
    }

    private void initView(){
        ViewPager vpChat = binding.vpChat;
        fragments = new ArrayList<>();
        fragments.add(new IndexFragment());
        fragments.add(new MyFragment());
        vpChat.setAdapter(new ViewPagerAdapter(getSupportFragmentManager(),fragments));
        vpChat.setOnPageChangeListener(this);
        vpChat.setCurrentItem(0);
        binding.imageView2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                vpChat.setCurrentItem(0);
            }
        });

        binding.imageView4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                vpChat.setCurrentItem(1);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        //fragment移动时的变化数据
    }

    @Override
    public void onPageSelected(int position) {
        if (position == 0){
            binding.imageView2.setActivated(true);
            binding.imageView4.setActivated(false);
        }
        if (position == 1){
            binding.imageView2.setActivated(false);
            binding.imageView4.setActivated(true);
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}